package com.xiaofang.serivce.imp;

import com.xiaofang.mapper.UserMapper;
import com.xiaofang.pojo.User;
import com.xiaofang.serivce.UserSerivc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @program: xiaofangbackend
 * @ClassName UserSericeImp
 * @description:
 * @author: aodi
 * @create: 2025-06-19 11:22
 * @Version 1.0
 */
@Service
public class UserSericeImp implements UserSerivc {

    @Autowired
    private UserMapper userMapper ;
    //    注册用户
    @Transactional(propagation = Propagation.REQUIRED , rollbackFor = Exception.class)
    @Override
    public Integer regUser(User user) {

//       查询用户的唯一型

        User itemUser = new User();
         itemUser.setUserName(user.getUserName());
        User user1 = this.userMapper.getUser(itemUser);
        if (user1 != null){
            throw new RuntimeException("用户已存在");
        }
       user.setCreateData(new java.util.Date());
        Integer i = this.userMapper.addUser(user);
        return i;

    }

    // 用户登入查询
    @Override
    @Transactional(rollbackFor = Exception.class , readOnly = true)
    public User getUser(User user) {
        User user1 = userMapper.getUser(user);

        if (user1 != null){
            return user1;
        }
        throw new RuntimeException("用户不存在");
    }

    @Override
    public List<User> getAllUser() {

        List<User> allUser = userMapper.getAllUser();
        return allUser;
    }
}
